package com.bc.bczbweb.controller;

import com.bc.bcapi.service.T_Authority_infoService;
import com.bc.bcapi.service.T_Menu_infoService;
import com.bc.bccore.util.PageHelper;
import com.bc.bcmodel.domain.T_Authority_infoModel;
import com.bc.bcmodel.domain.T_Menu_infoModel;
import com.bc.bcapi.service.T_Role_infoService;
import com.bc.bcmodel.domain.T_Role_infoModel;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("t_role_info")
public class T_Role_infoController extends BaseController {
    @Autowired
    T_Role_infoService t_Role_infoService;

    @Autowired
    T_Authority_infoService t_Authority_infoService;

    @Autowired
    T_Menu_infoService t_menu_infoService;

    @RequestMapping("list")
    public ModelAndView list() {
        ModelAndView view = super.baseView("列表", "t_role_info/list");
        return view;
    }

    @RequestMapping(value = "listdo", method = RequestMethod.POST)
    public ModelAndView listdo(T_Role_infoModel req) {
        ModelAndView view = super.baseViewAjax("列表", "t_role_info/listdo");
        PageInfo<T_Role_infoModel> pageInfo = t_Role_infoService.getList(req);
        view.addObject("data", pageInfo.getList());
        view.addObject("page", PageHelper.GetAjaxHtml(pageInfo, "javascript:loadpage(##);"));
        return view;
    }

    @RequestMapping("add")
    public ModelAndView add(String role_id) {
        String title = "添加";
        ModelAndView view = super.baseView(title, "t_role_info/add");
        T_Role_infoModel req = new T_Role_infoModel();
        req.setRole_id(role_id);
        T_Role_infoModel model = null;
        if (!role_id.equals("")) {
            title = "编辑";
            model = t_Role_infoService.findByNamedParam(req);
        }
        view.addObject("data", model);

        T_Authority_infoModel t_authority_infoModel = new T_Authority_infoModel();
        t_authority_infoModel.setRole_id(role_id);
        List<T_Authority_infoModel> listAuth = t_Authority_infoService.getAllList(t_authority_infoModel);
        T_Menu_infoModel menuModel = new T_Menu_infoModel();
        List<T_Menu_infoModel> listMenu = t_menu_infoService.getAllList(menuModel);
        String tree_data = GetTree(listMenu, listAuth);
        view.addObject("title", title);
        view.addObject("tree_data", tree_data);
        return view;
    }

    public String GetTree(List<T_Menu_infoModel> listMenu, List<T_Authority_infoModel> listAuth) {
        String str = "";
        for (T_Menu_infoModel item : listMenu) {
            String selected = "";
            String open = "";
            if (item.getP_id().equals("")) {
                open = " ,open: true ";
            }
            if (listAuth != null && listAuth.size() > 0) {
                int count = listAuth.stream().filter(t -> t.getMenu_id().equals(item.getMenu_id())).
                        collect(Collectors.toList()).size();
                if (count > 0) {
                    selected = " ,checked:true ";
                }
            }
//            JsonUtil
            str += "{ id: '" + item.getMenu_id() + "', pId: '" + item.getP_id() + "', name: '" + item.getMenu_name() + "' " + open + " " + selected + " },";
        }

        str = str.equals("") ? "" : str.substring(0, str.length() - 1);
        return str;
    }
}